- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[NFC][SpecialCaseList] Move "LongestMatch" logic from WarningsSpecialCaseList into SpecialCaseList #162409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            vitalybuka
  merged 5 commits into
  main
from
users/vitalybuka/spr/nfcspecialcaselist-move-longestmatch-logic-from-warningsspecialcaselist-into-specialcaselist
  
      
      
   
  Oct 8, 2025 
      
    
                
     Merged
            
            
          
      
        
          +26
        
        
          −38
        
        
          
        
      
    
  
Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    Created using spr 1.3.6 [skip ci]
Created using spr 1.3.6
| @llvm/pr-subscribers-llvm-support @llvm/pr-subscribers-clang Author: Vitaly Buka (vitalybuka) ChangesThis way we can be more flexible optimizing SpecialCaseList internals. Full diff: https://github.com/llvm/llvm-project/pull/162409.diff 2 Files Affected: 
 diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index 2b89370a42d1b..a955c3bca337f 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -517,12 +517,6 @@ class WarningsSpecialCaseList : public llvm::SpecialCaseList {
                         const SourceManager &SM) const;
 
 private:
-  // Find the longest glob pattern that matches FilePath amongst
-  // CategoriesToMatchers, return true iff the match exists and belongs to a
-  // positive category.
-  bool globsMatches(const llvm::StringMap<Matcher> &CategoriesToMatchers,
-                    StringRef FilePath) const;
-
   llvm::DenseMap<diag::kind, const Section *> DiagToSection;
 };
 } // namespace
@@ -584,43 +578,24 @@ void DiagnosticsEngine::setDiagSuppressionMapping(llvm::MemoryBuffer &Input) {
 bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId,
                                                SourceLocation DiagLoc,
                                                const SourceManager &SM) const {
+  PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc);
+  if (!PLoc.isValid())
+    return false;
   const Section *DiagSection = DiagToSection.lookup(DiagId);
   if (!DiagSection)
     return false;
-  const SectionEntries &EntityTypeToCategories = DiagSection->Entries;
-  auto SrcEntriesIt = EntityTypeToCategories.find("src");
-  if (SrcEntriesIt == EntityTypeToCategories.end())
+
+  StringRef F = llvm::sys::path::remove_leading_dotslash(PLoc.getFilename());
+
+  StringRef LongestSup = DiagSection->getLongestMatch("src", F, "");
+  if (LongestSup.empty())
     return false;
-  const llvm::StringMap<llvm::SpecialCaseList::Matcher> &CategoriesToMatchers =
-      SrcEntriesIt->getValue();
-  // We also use presumed locations here to improve reproducibility for
-  // preprocessed inputs.
-  if (PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc); PLoc.isValid())
-    return globsMatches(
-        CategoriesToMatchers,
-        llvm::sys::path::remove_leading_dotslash(PLoc.getFilename()));
-  return false;
-}
 
-bool WarningsSpecialCaseList::globsMatches(
-    const llvm::StringMap<Matcher> &CategoriesToMatchers,
-    StringRef FilePath) const {
-  StringRef LongestMatch;
-  bool LongestIsPositive = false;
-  for (const auto &Entry : CategoriesToMatchers) {
-    StringRef Category = Entry.getKey();
-    const llvm::SpecialCaseList::Matcher &Matcher = Entry.getValue();
-    bool IsPositive = Category != "emit";
-    for (const auto &Glob : Matcher.Globs) {
-      if (Glob->Name.size() < LongestMatch.size())
-        continue;
-      if (!Glob->Pattern.match(FilePath))
-        continue;
-      LongestMatch = Glob->Name;
-      LongestIsPositive = IsPositive;
-    }
-  }
-  return LongestIsPositive;
+  StringRef LongestEmit = DiagSection->getLongestMatch("src", F, "emit");
+  if (LongestEmit.empty())
+    return true;
+
+  return LongestSup.size() > LongestEmit.size();
 }
 
 bool DiagnosticsEngine::isSuppressedViaMapping(diag::kind DiagId,
diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp
index 6ad8d7d4e7ffa..7a4b7461b999a 100644
--- a/llvm/lib/Support/SpecialCaseList.cpp
+++ b/llvm/lib/Support/SpecialCaseList.cpp
@@ -262,4 +262,17 @@ unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
   return LastLine;
 }
 
+StringRef SpecialCaseList::Section::getLongestMatch(StringRef Prefix,
+                                                    StringRef Query,
+                                                    StringRef Category) const {
+  StringRef LongestRule;
+  if (const Matcher *M = findMatcher(Prefix, Category)) {
+    M->match(Query, [&](StringRef Rule, unsigned) {
+      if (LongestRule.size() < Rule.size())
+        LongestRule = Rule;
+    });
+  }
+  return LongestRule;
+}
+
 } // namespace llvm
 | 
              
                    qinkunbao
  
              
              approved these changes
              
                  
                    Oct 8, 2025 
                  
              
              
            
            
    
  svkeerthy 
      pushed a commit
      that referenced
      this pull request
    
      Oct 9, 2025 
    
    
      
  
    
      
    
  
…CaseList into SpecialCaseList (#162409) This way we can be more flexible optimizing SpecialCaseList internals.
    
  clingfei 
      pushed a commit
        to clingfei/llvm-project
      that referenced
      this pull request
    
      Oct 10, 2025 
    
    
      
  
    
      
    
  
…CaseList into SpecialCaseList (llvm#162409) This way we can be more flexible optimizing SpecialCaseList internals.
    
  vitalybuka 
      added a commit
      that referenced
      this pull request
    
      Oct 14, 2025 
    
    
      
  
    
      
    
  
* Test reordering of rules. * Test same length rules for emit and suppress. Follow up to #162409, to confirm it does not affect this behavior.
    
  akadutta 
      pushed a commit
        to akadutta/llvm-project
      that referenced
      this pull request
    
      Oct 14, 2025 
    
    
      
  
    
      
    
  
…CaseList into SpecialCaseList (llvm#162409) This way we can be more flexible optimizing SpecialCaseList internals.
    
  akadutta 
      pushed a commit
        to akadutta/llvm-project
      that referenced
      this pull request
    
      Oct 14, 2025 
    
    
      
  
    
      
    
  
…3277) * Test reordering of rules. * Test same length rules for emit and suppress. Follow up to llvm#162409, to confirm it does not affect this behavior.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
      Labels
      
    clang:frontend
  Language frontend issues, e.g. anything involving "Sema" 
  
    clang
  Clang issues not falling into any other category 
  
    llvm:support
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This way we can be more flexible optimizing SpecialCaseList internals.